package com.example.shan307.myapplication;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Window;

import com.baidu.location.LocationClient;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapView;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;

public class MainActivity extends Activity {
    private MapView mMapView;
    private BaiduMap mBaiduMap;
    // 定位相关
    private LocationClient mLocationClient;
    private MyLocationListener mlocationListener;
    private boolean isFirstIn = true;
    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);// 去除titlebar
        // 在使用SDK各组件之前初始化context信息，传入ApplicationContext
        // 注意该方法要再setContentView方法之前实现
        SDKInitializer.initialize(getApplicationContext());
        setContentView(R.layout.baidu);
        this.context = context;
        initView();// 初始化地图
        initLocation();// 初始化定位
    }

    private void initLocation() {
        mLocationClient = new LocationClient(this);
        mlocationListener = new MyLocationListener();
        mLocationClient.registerLocationListener(mlocationListener);

        LocationClientOption option = new LocationClientOption();// 定位配置
        option.setCoorType("gcj02");// 类型
        option.setIsNeedAddress(true);//
        option.setOpenGps(true);// 返回当前位置
        option.setScanSpan(1000);// 1s请求一次

        mLocationClient.setLocOption(option);
    }

    private void initView() {
        mMapView = (MapView) findViewById(R.id.bmapView);
        mBaiduMap = mMapView.getMap();

        MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(15.0f);
        mBaiduMap.setMapStatus(msu);// 设置地图比例为500米
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        // 在activity执行onResume时执行mMapView. onResume ()，实现地图生命周期管理
        mMapView.onResume();
    }

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        mBaiduMap.setMyLocationEnabled(true);// 开启定位允许
        if (!mLocationClient.isStarted()) {// 如果定位没开启
            mLocationClient.start();// 开始定位
        }

    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        // 在activity执行onPause时执行mMapView. onPause ()，实现地图生命周期管理
        mMapView.onPause();
    }

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
        // 停止定位
        mBaiduMap.setMyLocationEnabled(false);
        mLocationClient.stop();
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        // 在activity执行onDestroy时执行mMapView.onDestroy()，实现地图生命周期管理
        mMapView.onDestroy();
    }

    private class MyLocationListener implements BDLocationListener {

        @Override
        public void onReceiveLocation(BDLocation location) {

            MyLocationData data = new MyLocationData.Builder()//
                    .accuracy(location.getRadius())//
                    .latitude(location.getLatitude())//
                    .longitude(location.getLongitude()).build();
            mBaiduMap.setMyLocationData(data);

            if (isFirstIn) {
                // 获取经纬度
                LatLng latLng = new LatLng(location.getLatitude(),
                        location.getLongitude());
                MapStatusUpdate mus = MapStatusUpdateFactory.newLatLng(latLng);
                mBaiduMap.animateMapStatus(mus);
                isFirstIn = false;
            }
        }
    }
}